/**
 * pharma-model - 9 janv. 2011 guillaume
 */
package org.bspv.pharma.model.security;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.bspv.pharma.model.User;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

/**
 * @author guillaume
 */
public class SecurityUser extends User implements UserDetails {

    /**
     * serialVersionUID.
     */
    private static final long serialVersionUID = -2265933084760154956L;

    /**
     * Liste de droits.
     */
    private List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();

    /**
     * Constructeur par défaut.
     */
    public SecurityUser() {
        super();
    }

    /**
     * Constructeur prenant un {@link User} en paramètre.
     *
     * @param user
     *            Utilisateur
     */
    public SecurityUser(final User user) {
        super();
        this.setId(user.getId());
        this.setUsername(user.getUsername());
        this.setPassword(user.getPassword());
        this.setFirstname(user.getFirstname());
        this.setName(user.getName());
        this.setRank(user.getRank());
        this.setEmail(user.getEmail());
        this.setPhone(user.getPhone());
        this.setMobile(user.getMobile());
        this.setFireStation(user.getFireStation());
        this.setValidAccount(user.getValidAccount());
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#getAuthorities()
     * @return {@link Collection} de {@link GrantedAuthority}
     */
    public final Collection<GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonExpired ()
     * @return Booleen
     */
    public final boolean isAccountNonExpired() {
        return this.getValidAccount().booleanValue();
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isAccountNonLocked()
     * @return Booleen
     */
    public final boolean isAccountNonLocked() {
        return true;
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isCredentialsNonExpired ()
     * @return Booleen
     */
    public final boolean isCredentialsNonExpired() {
        return true; // voir pour une prochaine version
    }

    /**
     * @see org.springframework.security.userdetails.UserDetails#isEnabled()
     * @return Booleen
     */
    public final boolean isEnabled() {
        return this.getValidAccount().booleanValue();
    }

    /**
     * @param authorities
     *            the authorities to set
     */
    public final void setAuthorities(final List<GrantedAuthority> authorities) {
        this.authorities = authorities;
    }

}
